package xyz.ddlnt.serviceusr.controller;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import xyz.ddlnt.commonutil.enumeration.ResultCode;
import xyz.ddlnt.commonutil.result.Result;
import xyz.ddlnt.model.dto.LoginDTO;
import xyz.ddlnt.model.dto.UserInfoDTO;
import xyz.ddlnt.model.entity.UserInfo;
import xyz.ddlnt.model.vo.BloggerInfoVO;
import xyz.ddlnt.model.vo.UserAchieveVO;
import xyz.ddlnt.serviceusr.service.UserService;

/**
 * @author ZZULI_SE 210910
 * @data 2025/5/6 22:45
 */
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<String > login(@RequestBody LoginDTO loginDTO) {
        Boolean isSuccess = userService.login(loginDTO);
        return isSuccess ? Result.success(StpUtil.getTokenValue()) : Result.fail(ResultCode.USER_PASSWORD_ERROR);
    }

    /**
     * 用户注册
     * @param loginDTO
     * @return
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody LoginDTO loginDTO) {
        Boolean isRegister =userService.register(loginDTO);
        return Result.success(isRegister);
    }

    /**
     * 用户注销
     * @param loginDTO
     * @return
     */
    @PostMapping("/cancel")
    public Result<Boolean> cancel(@RequestBody LoginDTO loginDTO) {
        Boolean isCancel =userService.cancel(loginDTO);
        return Result.success(isCancel);
    }

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result<UserInfo> getUserInfo(@RequestParam Long id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 修改用户信息
     * @param userInfoDTO
     * @return
     */
    @PostMapping("/updateUserInfoById")
    public Result<Boolean> updateUserInfoById(@RequestBody UserInfoDTO userInfoDTO) {
        return Result.success(userService.updateUserInfoById(userInfoDTO));
    }

    /**
     * 获取博主信息
     * @param id
     * @return
     */
    @GetMapping("/getBloggerInfo")
    public Result<UserInfo> getBloggerInfo(@RequestParam Long id) {
        // TODO 获取博主信息
        return Result.success(userService.getById(id));
    }

    /**
     * 获取文章的博主信息
     * @param id
     * @return
     */
    @GetMapping("/getPostBloggerInfo")
    public Result<BloggerInfoVO> getPostBloggerInfo(@RequestParam Long id) {
        return Result.success(userService.getPostBloggerInfo(id));
    }

    /**
     * 获取用户成就信息
     * @param id
     * @return
     */
    @GetMapping("/getUserAchieve")
    public Result<UserAchieveVO> getUserAchieve(@RequestParam Long id) {
        return Result.success(userService.getUserAchieve(id));
    }
}
